<?php
//递归, 将树型结构变为一维数组, 并添加id
function admin_menu_arr($tree, $pid=0){
    $temp = [];
    foreach($tree as $k => $v){
        $temp[] = [
            "id"  => $pid."_".$k,
            "pid" => $pid,
            "title"=>$v['title'],
            "font" => "larry-icon",
            "icon"=> isset($v['icon']) ? $v['icon'] : "",
            "url"=>$v['url'] ? strtolower($v['url']) : $pid."_".$k,
        ];
        if(isset($v['children'])){
            $return = admin_menu_arr($v['children'], $pid."_".$k);
            $temp = array_merge($temp,$return);
        }
    }
    return $temp;
}

//递归, 将一维数组还原为前端需要的结构
function admin_menu_tree($temp,$pid=0){
    $tree = [];
    foreach($temp as $k => $v){
        if($v['pid'] === $pid){
            unset($temp[$k]);
            $v['children'] = admin_menu_tree($temp,$v['id']);
            if(!$v['children']){
                unset($v['children']);
            }
            $tree[] = $v;
        }
    }
    return $tree;
}
//递归, 输出为html
function admin_menu_html($tree)
{
    $html = '';
    foreach($tree as $v)
    {
      if(!isset($v['children']))
      {
          $arr = explode("/",$v['url']);
          $c = $arr[1];
          $html .= "<li>
          <label><input _url='{$v['url']}' type='checkbox' lay-ignore name='content[]' value='{$v['url']}' /> {$v['title']}</label>
          <label><input _url='/{$c}/add' type='checkbox' lay-ignore name='content[]' value='/{$c}/add' /> 添加</label>
          <label><input _url='/{$c}/edit' type='checkbox' lay-ignore name='content[]' value='/{$c}/edit' /> 编辑</label>
          <label><input _url='/{$c}/del' type='checkbox' lay-ignore name='content[]' value='/{$c}/del' /> 删除</label>
          <label><input _url='/{$c}/detail' type='checkbox' lay-ignore name='content[]' value='/{$c}/detail' /> 查看</label>
          </li>";
      }
      else
      {
           $html .= "<li><label><input class='all' _url='{$v['url']}' type='checkbox' lay-ignore name='content[]' value='{$v['url']}' /> {$v['title']}</label>";
           $html .= admin_menu_html($v['children']);
           $html = $html."</li>";
      }
    }
    return $html ? '<ul>'.$html.'</ul>' : $html ;
}
